package com.douding.file.controller.admin;

import com.douding.server.dto.FileDto;
import com.douding.server.dto.ResponseDto;
import com.douding.server.service.FileService;
import com.douding.server.service.TestService;
import com.douding.server.util.Base64ToMultipartFile;
import com.douding.server.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

import java.nio.file.Files;
import java.nio.file.Paths;


/*
    返回json 应用@RestController
    返回页面  用用@Controller
 */
@RequestMapping("/admin/file")
@RestController
public class UploadController {

    private static final Logger LOG = LoggerFactory.getLogger(UploadController.class);
    public  static final String BUSINESS_NAME ="文件上传";
    @Resource
    private TestService testService;

    @Value("${file.path}")
    private String FILE_PATH;

    @Value("${file.domain}")
    private String FILE_DOMAIN;

    @Resource
    private FileService fileService;

    @RequestMapping("/upload")
    public ResponseDto upload(@RequestBody FileDto fileDto) throws Exception {
        System.out.println(fileDto);
        //创建ResponseDto对象
        ResponseDto<FileDto> responseDto = new ResponseDto<>();
        //判断传入的base64码
        if (fileDto.getShard() == null) {
            // 图像数据为空
            responseDto.setSuccess(false);
            return responseDto;
        }

        //获取base64码
        String baseStr = fileDto.getShard();

        //base64码转化为MultipartFile对象
        MultipartFile multipartFile = Base64ToMultipartFile.base64ToMultipart(baseStr);


        // 获取文件类型
        String suffix = fileDto.getSuffix();
        //文件名拼接
        String fileName = UuidUtil.getUuid() + "." + suffix;
        // 拼接文件的全路径
        String fullPath = FILE_PATH +
                // 拼接文件名
                fileName;

        //获取项目本身路径
        // 如果不存在文件保存的位置
        if (!Files.exists(Paths.get(FILE_PATH))) {
            File file = new File(FILE_PATH);
            file.mkdirs();
            // 创建文件夹
        }

        // 保存上传的文件
        multipartFile.transferTo(new File(fullPath));

        // 定义返回的路径
        fileDto.setPath(FILE_DOMAIN + fileName);
        responseDto.setSuccess(true);
        responseDto.setContent(fileDto);


        return responseDto;
    }

    //合并分片
    public void merge(FileDto fileDto) throws Exception {
        LOG.info("合并分片开始");

    }

    @GetMapping("/check/{key}")
    public ResponseDto check(@PathVariable String key) throws Exception {
        //创建ResponseDto对象
        ResponseDto<FileDto> responseDto = new ResponseDto<>();
        LOG.info("检查上传分片开始：{}", key);
        //判断传递过来的key值是否为空，如果为空，ResponseDto对象是否成功的值返回为false
        if (key == null){
            responseDto.setSuccess(false);
            return responseDto;
        }

        //通过传递进来的key查询数据库中的内容
        FileDto fileDto = fileService.findByKey(key);
        //将查询到的内容（不管是否存在），存入响应的content部分
        responseDto.setContent(fileDto);

        responseDto.setSuccess(true);
        //返回响应
        return responseDto;
    }

}//end class
